<template>
  <view class="tab_bar_box">
    <view class="tab_bar_list">
      <view
        v-for="(item, index) in list"
        :key="index"
        class="tab_bar_item"
        :data-path="item.pagePath"
        :data-index="index"
        :data-typed="item.typed"
        :data-text="item.text"
        @click="to"
      >
        <view class="item" v-if="item.typed">
          <uni-icons
            :type="selected === index ? item.selectedIconPath : item.iconPath"
            :color="selected === index ? selectedColor : color"
            size="28"
          />
          <view
            :style="{ color: selected === index ? selectedColor : color }"
            class="msg item"
            >{{ item.text }}</view
          >
        </view>

        <view v-else class="door item">
          <view class="door_open">
            <uni-icons type="locked" color="#fff" size="30" />
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<!-- 底部tabBar导航栏组件 -->
<script lang="ts" setup>
import { onShow } from '@dcloudio/uni-app'
import { ref } from 'vue'
//当前选中的下标
const selected = ref(0)
//未选中图标、字体颜色
const color = ref('rgb(101, 96, 96)')
//选中图标、字体颜色
const selectedColor = ref('rgb(239, 143, 26)')
const list = ref([
  {
    pagePath: '/pages/index/index',
    iconPath: 'shop',
    selectedIconPath: 'shop-filled',
    text: '房源',
    typed: true,
  },
  {
    pagePath: '/pages/life/life',
    iconPath: 'gift',
    selectedIconPath: 'gift-filled',
    text: '生活',
    typed: true,
  },
  {
    pagePath: '/pages/life/life',
    iconPath: 'gift',
    selectedIconPath: 'gift-filled',
    text: '门锁打开',
    typed: false,
  },
  {
    pagePath: '/pages/community/community',
    iconPath: 'vip',
    selectedIconPath: 'vip-filled',
    text: '社区',
    typed: true,
  },
  {
    pagePath: '/pages/my/my',
    iconPath: 'staff',
    selectedIconPath: 'staff-filled',
    text: '我的',
    typed: true,
  },
])

//跳转页面
const to = (e: any) => {
  //获取元素上的自定义属性
  const data = e.currentTarget.dataset
  if (!data.typed) {
    uni.showToast({
      title: data.text,
      icon: 'none',
      mask: true
    })
    return
  }

  //缓存下标
  uni.setStorageSync('tabBarIndex', data.index + '')
  //切换页面
  uni.switchTab({ url: data.path })
}

//页面显示
onShow(() => {
  //获取选中下标
  getTabBarIndex()
})

//获取当前选中的下标
const getTabBarIndex = () => {
  //获取当前选中下标
  let index = uni.getStorageSync('tabBarIndex')
  //不为空每个则更新选中下标
  if (index != null && index != '') {
    selected.value = parseInt(index)
  }
}
</script>

<style scoped lang="scss">
.tab_bar_box {
  width: 100%;
  height: 110rpx;
  border-top: 2px solidrgb(209, 205, 205) d;
  background-color: $uni-bg-color;

  .tab_bar_list {
    width: 100%;
    position: fixed;
    bottom: 0;
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    align-items: center;
    justify-content: center;
    background-color: $uni-bg-color;

    .tab_bar_item {
      display: flex;
      height: 100%;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      font-size: 28rpx;

      .item {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
      }

      .door {
        width: 110rpx;
        height: 110rpx;
        border-radius: 50%;
        transform: translateY(-35%);
        display: flex;
        align-items: center;
        justify-content: space-around;
        background-color: $uni-bg-color;

        .door_open {
          width: calc(100% - 20rpx);
          height: calc(100% - 20rpx);
          border-radius: 50%;
          display: flex;
          align-items: center;
          justify-content: center;
          background-color: $uni-bg-color-mask;
          background: linear-gradient(
            rgb(249, 159, 50),
            rgba(239, 143, 26, 0.426)
          );
        }
      }
    }
  }
}

</style>
